<?php
namespace app\index\controller;

use think\Request;
use think\Db;
use think\Loader;

class Index
{
    public static $num = 10;
    public static $money = 3;

    public function index()
    {

        //sh /usr/local/silkv3/converter.sh /home/wwwroot/xcx.sswan666.top/tp5/test.silk wav
        //TODO 动态生成语音文件
        $name = 'haha';
        $silk_path = ROOT_PATH."{$name}.silk";
        $wav_path = ROOT_PATH."{$name}.wav";
        if (!is_file($wav_path)){
            $this->siliToWav($silk_path);
        }

        Loader::import('speech.AipSpeech', VENDOR_PATH,'.php');
        $aipSpeech = new \AipSpeech( config('sppech.appid'),config('sppech.api_key'),config('sppech.secret_key') );

        // 识别本地文件
        $result = $aipSpeech->asr(file_get_contents($wav_path), 'wav', 16000, array(
            'lan' => 'zh',
        ));
        $re_res =  $aipSpeech->asr(file_get_contents(ROOT_PATH.'haha.wav'), 'wav', 16000, array(
            'lan' => 'zh',
        ));

        if ($result['err_no'] == 0) {
            $voicesResult = json_encode($result['result'], JSON_UNESCAPED_UNICODE);
            $re_resResult = json_encode($re_res['result'], JSON_UNESCAPED_UNICODE);

        }
        var_dump($voicesResult,$re_resResult);


    }

    function siliToWav($path){
        return exec("bash /usr/local/silkv3/converter.sh ".ROOT_PATH.$path." wav");
    }


}
